﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LsMisCommon;
using WeifenLuo.WinFormsUI.Docking;

namespace card
{
    public partial class SRCHDJ : DockContent
    {
        public static WebDG.Service ws = new card.WebDG.Service();
        bool is_new_card = false;
        bool is_update = false;
        string update_id = "";
        string previous_car_no = "";//previous car NO
        string stack_type = "";
        //following variable is used to store the previous record
        string bill_no_copy = "";
        string material_copy = "";
        string specification_copy = "";
        string pieces_copy = "";
        string tons_copy = "";
        string squares_copy = "";
        string yard_copy = "";
        string remark_copy = "";
        string owner_copy = "";

        public DataSet shippers
        {
            get;
            set;
        }

        //定义自动刷新的delegate
        delegate void DataGridTruckLoginHandler(DataTable dt);
        event DataGridTruckLoginHandler TruckLogin;

        delegate void UncheckLabelRereshHandler(DataTable uncheck_cars);
        event UncheckLabelRereshHandler UncheckRefresh;

        public SRCHDJ()
        {
            InitializeComponent();
        }

        static String interval = System.Configuration.ConfigurationManager.AppSettings["refresh_interval"];
        static int refresh_interval = int.Parse(interval) * 60000;
        System.Timers.Timer t = new System.Timers.Timer(refresh_interval);//实例化Timer类，设置间隔时间为60秒；
        //System.Timers.Timer t = new System.Timers.Timer(60000);//实例化Timer类，设置间隔时间为60秒；

        string card_add = "";
        private void SRCHDJ_Load(object sender, EventArgs e)
        {
            //获得发卡地点
            card_add = System.Configuration.ConfigurationManager.AppSettings["card_address"];

            this.Text = "市入车货登记--" + card_add;  

            string dtime = ws.GetSysDateTime().ToString();
            this.textBox_loginTime.Text = dtime; 

            this.comboBox_owner.DataSource = Session.Shippers.Tables[0];
            this.comboBox_owner.ValueMember = "SHIPPER_NAME"; 
            this.comboBox_owner.DisplayMember = "DESCRIPTION";

            string sql_shippers = "select SHIPPER_NAME,SHIPPER_BRIEF,SHIPPER_BRIEF||'/'||SHIPPER_SHOT as DESCRIPTION from V_SHIPPER_DOC_TCC";
            DataSet ds_shippers = ws.getDataSet_Serv(sql_shippers);
            this.ComboBox_getOwner.DataSource = ds_shippers.Tables[0];
            this.ComboBox_getOwner.ValueMember = "SHIPPER_NAME";
            this.ComboBox_getOwner.DisplayMember = "DESCRIPTION";

            //Set the datasource for 
            this.comboBox_stackName.DataSource = Session.Cargo_names.Tables[0];
            this.comboBox_stackName.ValueMember = "CARGO_NAM";
            this.comboBox_stackName.DisplayMember = "DISPLAYNAME";

            this.comboBox_owner.Text = "";
            this.ComboBox_getOwner.Text = "";

            //Set the car no select groupbox invisible
            lab_car_ok.Text = "";
            groupBox3.Visible = false;

            this.textBox_carNO.Focus();

            UncheckRefresh += new UncheckLabelRereshHandler(fresh_uncheck);
            //定义列表自动刷新
            TruckLogin += new DataGridTruckLoginHandler(Add_DataTable);
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件；
            t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件； 

            load_data();
        }

        void Add_DataTable(DataTable dt)
        {

            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.DataSource = dt;

            //设置某些字段不可见
            this.dataGridView1.Columns["IOYARDID"].Visible = false;
            this.dataGridView1.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView1.Columns["ID"].Visible = false;
        }
        void fresh_uncheck(DataTable dt)
        {
            this.label_uncheck.Text = dt.Rows[0][0].ToString();
        }
        public void theout(object source, System.Timers.ElapsedEventArgs e)
        {
            sx();
            //string car_no = this.textBox_carNO.Text.ToString().Trim();
            //string sql = "select logintime as 登记时间,CARDTIME as 确认时间,SHIPPER_NAME as 货主代理,BILL_NO as 提单号,cargoname as 货名,truckno as  车号,qdcardnum as 卡号,DECODE(to_number(TRUCKSTATUS),0,'登记',1,'发指令',2,'确认',3,'已进门',4,'已理货',5,'已出门') as 状态,pieces as 件数,DRIVERPHONE as 电话,cardadd as 登记地点,IOYARDID,TRUCKSTATUS,ID,DYBZ,YARD AS 地点,ZYJX AS 机械 from TRUCK_CARGO_LOGIN" +
            //    " where (TRUCKSTATUS='0' or TRUCKSTATUS='1' or TRUCKSTATUS='2') and ioyardid=1 and cardadd='" + card_add + "'order by TRUCKSTATUS,logintime desc";
            //DataSet ds = ws.getDataSet_Serv(sql);
            //string sql_un = "select count(*) as un_check from TRUCK_CARGO_LOGIN where TRUCKSTATUS='1' and IOYARDID='1'and cardadd='" + card_add + "'";
            //DataSet ds_uncheck = ws.getDataSet_Serv(sql_un);
            //if (this.IsHandleCreated)
            //{
            //    this.dataGridView1.BeginInvoke(TruckLogin, ds.Tables[0]);
            //    this.label_uncheck.BeginInvoke(UncheckRefresh, ds_uncheck.Tables[0]);
            //}
        }


        private void sx()
        {
            string car_no = this.textBox_carNO.Text.ToString().Trim();
            string sql = "select logintime as 登记时间,CARDTIME as 确认时间,SHIPPER_NAME as 货主代理,BILL_NO as 提单号,cargoname as 货名,truckno as  车号,qdcardnum as 卡号,DECODE(to_number(TRUCKSTATUS),0,'登记',1,'发指令',2,'确认',3,'已进门',4,'已理货',5,'已出门') as 状态,pieces as 件数,DRIVERPHONE as 电话,cardadd as 登记地点,IOYARDID,TRUCKSTATUS,ID,DYBZ,YARD AS 地点,ZYJX AS 机械 from TRUCK_CARGO_LOGIN" +
                " where (TRUCKSTATUS='0' or TRUCKSTATUS='1' or (TRUCKSTATUS='2' and CARDTIME>=SYSDATE-1.5)) and ioyardid=1 and cardadd='" + card_add + "'order by TRUCKSTATUS,logintime desc";
            DataSet ds = ws.getDataSet_Serv(sql);
            string sql_un = "select count(*) as un_check from TRUCK_CARGO_LOGIN where TRUCKSTATUS='1' and IOYARDID='1'and cardadd='" + card_add + "'";
            DataSet ds_uncheck = ws.getDataSet_Serv(sql_un);
            if (this.IsHandleCreated)
            {
                this.dataGridView1.BeginInvoke(TruckLogin, ds.Tables[0]);
                this.label_uncheck.BeginInvoke(UncheckRefresh, ds_uncheck.Tables[0]);
            }
        }

        private void button_exist_Click(object sender, EventArgs e)
        {
            this.Close();//Close the current window, that is, "市入车货登记" window.
        }

        private void btn_car_Click(object sender, EventArgs e)
        {
            groupBox3.Left = this.textBox_carNO.Left;
            //groupBox3.Top = btn_car.Top + panel1.Top + btn_car.Height - groupBox1.Height;
            groupBox3.Top = this.textBox_carNO.Top + 80;
            lab_car_ok.Text = "";
            groupBox3.Visible = true;
            this.textBox_carNO.SelectionStart = this.textBox_carNO.Text.Length;
            this.textBox_carNO.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lab_car_ok.Text = "";
            groupBox3.Visible = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox_carNO.Text = "";
            this.textBox_carNO.SelectionStart = this.textBox_carNO.Text.Length;
            this.textBox_carNO.Focus();
        }
        #region
        private void label77_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label77.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label76_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label76.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label75_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label75.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label80_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label80.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label79_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label79.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label82_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label82.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label83_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label83.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label88_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label88.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label87_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label87.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label86_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label86.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label85_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label85.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label84_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label84.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label81_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label81.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label18_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label18.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label17_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label17.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label16_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label16.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label15_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label15.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label14_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label14.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label13_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label13.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label24_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label24.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label23_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label23.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label22_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label22.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label21_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label21.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label20_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label20.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label19_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label19.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label30_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label30.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label29_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label29.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label28_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label28.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label27_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label27.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label26_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label26.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label25_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label25.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label36_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label36.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label35_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label35.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label34_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label34.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label33_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label33.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label32_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label32.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label31_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label31.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label42_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label42.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label41_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label41.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label40_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label40.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label39_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label39.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label38_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label38.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label37_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label37.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label48_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label48.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label47_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label47.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label46_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label46.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label45_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label45.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label44_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label44.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label43_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label43.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label54_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label54.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label53_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label53.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label52_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label52.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label51_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label51.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label50_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label50.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label49_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label49.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label60_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label60.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label59_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label59.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label58_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label58.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label57_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label57.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label56_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label56.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label55_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label55.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label66_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label66.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label65_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label65.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label64_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label64.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label63_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label63.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label62_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label62.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label61_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label61.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label72_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label72.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label71_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label71.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label70_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label70.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label69_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label69.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label68_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label68.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label67_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label67.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label78_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label78.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }
        #endregion

        private void button_clear_Click(object sender, EventArgs e)
        {
            clear_all();
            is_update = false;
        }
        //Save the multiple record 
        private void button_multiple_Click(object sender, EventArgs e)
        {
            //检查车辆是否在黑名单中，黑名单的车不予保存
            string car_no = this.textBox_carNO.Text.ToString().Trim();
            string driver_identity = this.textBox_identity.Text.ToString().Trim();
            if (car_no.Length > 0 || driver_identity.Length > 0)
            {
                bool is_black = check_black();
                if (is_black)
                {
                    //MessageBox.Show("此车在黑名单中，不能登记!");
                    return;
                }
            }

            bool is_checked = check_input();//check the input data
            if (!is_checked)
            {
                return;
            }

            string billNo = this.textBox_billNO.Text.ToString();
            string cardNo = this.textBox_cardNO.Text.ToString();
            string carNo = this.textBox_carNO.Text.ToString();
            string comment = this.textBox_comment.Text.ToString();
            string materail = this.textBox_material.Text.ToString().ToUpper();
            string pieces = this.textBox_pieces.Text.ToString().Trim();
            if (pieces.Length == 0) pieces = "0";
            string specification = this.textBox_specification.Text.ToString();
            string squares = this.textBox_squares.Text.ToString().Trim();
            if (squares.Length == 0) squares = "0";
            string tons = this.textBox_tons.Text.ToString().Trim();
            if (tons.Length == 0) tons = "0";
            //string shipper = this.comboBox_owner.SelectedValue.ToString();
            string shipper = "";
            if (this.comboBox_owner.SelectedValue != null)
            {
                shipper = this.comboBox_owner.SelectedValue.ToString().Trim();
            }
            else
            {
                shipper = this.comboBox_owner.Text.ToString().Trim();
            }
            string getOwner = "";
            if (this.comboBox_owner.SelectedValue != null)
            {
                getOwner = this.ComboBox_getOwner.SelectedValue.ToString().Trim();
            }
            else
            {
                getOwner = this.ComboBox_getOwner.Text.ToString().Trim();
            }
            string stackName = this.comboBox_stackName.SelectedValue.ToString();
            string yard = this.textBox_yard.Text.ToString().Trim();
            string username = Session.Operator_User;

            if (carNo != previous_car_no)//the same car cargo
            {
                if (DispCarNo(carNo))
                {
                    MessageBox.Show("此车已在其他停车场登记，不能再次登记!");
                    return;
                }
                //login_time = this.textBox_loginTime.Text.ToString();
                string dtime = ws.GetSysDateTime().ToString();
                this.textBox_loginTime.Text = dtime;
                previous_car_no = carNo;
            }

            string driver_name = this.textBox_name.Text.ToString().Trim();
            string driver_phone = this.textBox_phone.Text.ToString().Trim();

            //Create ID 
            string id = create_id();
            string insert_sql = "insert into TRUCK_CARGO_LOGIN(ID,LOGINTIME,TRUCKNO,IOYARDID,CARGONAME,SHIPPER_NAME," +
                "PIECES,WEIGHT,STANDARDS,OPERATOR,TRUCKSTATUS,BILL_NO,MATERIAL,QDCARDNUM,CUBE,REMARK,YARD,DRIVERNAME,DRIVERID,DRIVERPHONE,GETOWNER,CARDADD) values(" + id +
                ",to_date('" + this.textBox_loginTime.Text.ToString() + "','YYYY-MM-DD HH24:MI:SS'),'" + carNo + "','1','" + stackName +
                "','" + shipper + "'," + pieces + "," + tons + ",'" + specification + "','" + username + "','0','" +
                billNo + "','" + materail + "','" + cardNo + "'," + squares + ",'" + comment + "','" + yard + "','" + driver_name + "','" + driver_identity + "','" + driver_phone + "','" + getOwner + "','" + card_add + "')";
            int ret = ws.execute_UID_Serv(insert_sql);
            clear_second_section();//Clear the second section input box content
            load_data();
            //seting values for copy
            bill_no_copy = billNo;
            material_copy = materail;
            specification_copy = specification;
            pieces_copy = pieces;
            tons_copy = tons;
            squares_copy = squares;
            yard_copy = yard;
            remark_copy = comment;
            owner_copy = shipper;

            MessageBox.Show("保存成功，请录入下一票数据！");
        }
        private bool DispCarNo(string _carno)
        {
            StringBuilder sbsql = new StringBuilder();
            sbsql.AppendFormat("SELECT ID FROM TRUCK_CARGO_LOGIN WHERE TRUCKNO ='{0}' AND  TRUCKSTATUS='0'", _carno.Trim().Trim(','));
            if (ws.getDataSet_Serv(sbsql.ToString()).Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //Save
        private void button_save_Click(object sender, EventArgs e)
        {
            //检查车辆是否在黑名单中，黑名单的车不予保存
            string car_no = this.textBox_carNO.Text.ToString().Trim();
            string driver_identity = this.textBox_identity.Text.ToString().Trim();
            if (car_no.Length > 0 || driver_identity.Length > 0)
            {
                bool is_black = check_black();
                if (is_black)
                {
                    //MessageBox.Show("此车在黑名单中，不能登记!");
                    return;
                }
            }

            if (DispCarNo(car_no))
            {
                MessageBox.Show("此车已在其他停车场登记，不能再次登记!");
                return;
            }

            bool is_checked = check_input();//check the input data
            if (!is_checked)
            {
                return;
            }
            if (is_new_card)
            {//if we'll create one new card, we should update the RFID_READ table at the same time.

            }
            string billNo = this.textBox_billNO.Text.ToString();
            string cardNo = this.textBox_cardNO.Text.ToString();
            string carNo = this.textBox_carNO.Text.ToString();
            string comment = this.textBox_comment.Text.ToString();
            string materail = this.textBox_material.Text.ToString().ToUpper();
            string pieces = this.textBox_pieces.Text.ToString();
            string specification = this.textBox_specification.Text.ToString();
            string squares = this.textBox_squares.Text.ToString().Trim();
            if (squares.Length == 0) squares = "0";
            string tons = this.textBox_tons.Text.ToString();
            //string shipper = this.comboBox_owner.SelectedValue.ToString();
            string shipper = "";
            if (this.comboBox_owner.SelectedValue != null)
            {
                shipper = this.comboBox_owner.SelectedValue.ToString().Trim();
            }
            else
            {
                shipper = this.comboBox_owner.Text.ToString().Trim();
            }
            string getOwner = "";
            if (this.comboBox_owner.SelectedValue != null)
            {
                getOwner = this.ComboBox_getOwner.SelectedValue.ToString().Trim();
            }
            else
            {
                getOwner = this.ComboBox_getOwner.Text.ToString().Trim();
            }
            string stackName = this.comboBox_stackName.SelectedValue.ToString();
            string yard = this.textBox_yard.Text.ToString().Trim();
            string username = Session.Operator_User;

            string driver_name = this.textBox_name.Text.ToString().Trim();
            string driver_phone = this.textBox_phone.Text.ToString().Trim();

            string dtime = ws.GetSysDateTime().ToString();
            this.textBox_loginTime.Text = dtime;

            if (carNo != previous_car_no)//the same car cargo
            {
                //login_time = this.textBox_loginTime.Text.ToString();
                //string dtime = DateTime.Now.ToString();
                previous_car_no = carNo;
            }

            if (is_update)
            {
                string sql_update = "update TRUCK_CARGO_LOGIN set TRUCKNO='" + carNo + "',CARGONAME='" + stackName + "',SHIPPER_NAME='" + shipper + "',PIECES=" + pieces + ",WEIGHT=" + tons
                    + ",STANDARDS='" + specification + "',BILL_NO='" + billNo + "',MATERIAL='" + materail + "',QDCARDNUM='" + cardNo + "',CUBE='" + squares + "',REMARK='" + comment +
                    "',DRIVERNAME='" + driver_name + "',DRIVERID='" + driver_identity + "',DRIVERPHONE='" + driver_phone + "',GETOWNER='" + getOwner + "',YARD='" + yard + "' where id='" + update_id + "'";
                ws.execute_UID_Serv(sql_update);
                is_update = false;
            }
            else
            {
                //Create ID 
                string id = create_id();
                string insert_sql = "insert into TRUCK_CARGO_LOGIN(ID,LOGINTIME,TRUCKNO,IOYARDID,CARGONAME,SHIPPER_NAME," +
                    "PIECES,WEIGHT,STANDARDS,OPERATOR,TRUCKSTATUS,BILL_NO,MATERIAL,QDCARDNUM,CUBE,REMARK,YARD,DRIVERNAME,DRIVERID,DRIVERPHONE,GETOWNER,CARDADD) values(" + id +
                    ",to_date('" + this.textBox_loginTime.Text.ToString() + "','YYYY-MM-DD HH24:MI:SS'),'" + carNo + "','1','" + stackName +
                    "','" + shipper + "'," + pieces + "," + tons + ",'" + specification + "','" + username + "','0','" +
                    billNo + "','" + materail + "','" + cardNo + "'," + squares + ",'" + comment + "','" + yard + "','" + driver_name + "','" + driver_identity + "','" + driver_phone + "','" + getOwner + "','" + card_add + "')";
                int ret = ws.execute_UID_Serv(insert_sql);
            }
            clear_all();//Clear all input box content

            //Set login time again
            //string dtime = DateTime.Now.ToString();
            //this.textBox_loginTime.Text = dtime;

            load_data();
            //seting values for copy
            bill_no_copy = billNo;
            material_copy = materail;
            specification_copy = specification;
            pieces_copy = pieces;
            tons_copy = tons;
            squares_copy = squares;
            yard_copy = yard;
            remark_copy = comment;
            owner_copy = shipper;

            MessageBox.Show("保存成功！");
        }
        //Clear all input box in the window
        private void clear_all()
        {
            this.textBox_billNO.Text = "";
            this.textBox_cardNO.Text = "";
            this.textBox_carNO.Text = "";
            this.textBox_comment.Text = "";
            this.textBox_material.Text = "";
            this.textBox_pieces.Text = "";
            this.textBox_specification.Text = "";
            this.textBox_squares.Text = "";
            this.textBox_tons.Text = "";
            this.textBox_yard.Text = "";
            this.comboBox_owner.Text = "";
            this.comboBox_stackName.SelectedValue = "";

            this.textBox_name.Text = "";
            this.textBox_identity.Text = "";
            this.textBox_phone.Text = "";
        }
        //Clear the second section input box content in the window
        private void clear_second_section()
        {
            this.textBox_billNO.Text = "";
            this.textBox_comment.Text = "";
            this.textBox_material.Text = "";
            this.textBox_pieces.Text = "";
            this.textBox_specification.Text = "";
            this.textBox_squares.Text = "";
            this.textBox_tons.Text = "";
            this.textBox_yard.Text = "";
        }
        //Create ID
        private string create_id()
        {
            string sequence = "";
            string sqlStr = "select SEQ_TRUCK.nextval from dual";
            DataSet ds1 = ws.getDataSet_Serv(sqlStr);
            if (ds1.Tables[0].Rows.Count > 0)
            {
                sequence = ds1.Tables[0].Rows[0][0].ToString();
            }
            string year_str = DateTime.Now.Year.ToString();
            string month_str = DateTime.Now.Month.ToString();
            string day_str = DateTime.Now.Day.ToString();
            //string hour_str = DateTime.Now.Hour.ToString();
            //string minute_str = DateTime.Now.Minute.ToString();
            //string second_str = DateTime.Now.Second.ToString();
            //tring id = year_str + month_str + day_str + hour_str + minute_str + second_str + sequence;
            string id = year_str + month_str + day_str + sequence;
            return id;
        }
        //check input data
        private bool check_input()
        {
            string car_no = this.textBox_carNO.Text.ToString().Trim();
            string card_no = this.textBox_cardNO.Text.ToString().Trim();
            //string shipper = this.comboBox_owner.SelectedValue.ToString().Trim();
            string driver_identity = this.textBox_identity.Text.ToString().Trim();
            string shipper = "";
            if (this.comboBox_owner.SelectedValue != null)
            {
                shipper = this.comboBox_owner.SelectedValue.ToString().Trim();
            }
            else
            {
                shipper = this.comboBox_owner.Text.ToString().Trim();
            }
            //
            if (driver_identity.Length != 18)
            {
                MessageBox.Show("身份证号位数应该为18位!");
                this.textBox_identity.Focus();
                return false;
            }
            string stack_name = this.comboBox_stackName.SelectedValue.ToString().Trim();
            string pieces = this.textBox_pieces.Text.ToString().Trim();
            string tons = this.textBox_tons.Text.ToString().Trim();
            if (car_no.Length == 0)
            {
                MessageBox.Show("车号不允许为空!");
                this.textBox_carNO.Focus();
                return false;
            }
            if (!check_car_no_length())//check the numbers of car no
            {
                MessageBox.Show("车号的位数不正确！");
                return false;
            }
            //if (card_no.Length == 0)
            //{
            //    MessageBox.Show("卡号不允许为空!");
            //    this.textBox_cardNO.Focus();
            //    return false;
            //}
            if (shipper.Length == 0)
            {
                MessageBox.Show("货主/代理不允许为空!");
                return false;
            }
            if (stack_name.Length == 0)
            {
                MessageBox.Show("货名不允许为空!");
                return false;
            }
            if (pieces.Length == 0)
            {
                MessageBox.Show("件数不允许为空!");
                this.textBox_pieces.Focus();
                return false;
            }
            if (tons.Length == 0)
            {
                MessageBox.Show("吨数不允许为空!");
                this.textBox_tons.Focus();
                return false;
            }
            return true;
        }

        #region check blacklist
        //Check the car whether is in black list
        private bool check_black()
        {
            string car_no = this.textBox_carNO.Text.ToString().Trim();
            if (check_black(car_no))
            {
                MessageBox.Show("此车在黑名单中，不能登记!");
                return true;
            }
            string identiry = this.textBox_identity.Text.ToString().Trim();
            if (check_black(identiry))
            {
                MessageBox.Show("此身份证号在黑名单中，不能登记!");
                return true;
            }
            return false;
        }

        private bool check_black(string truckNo)
        {
            string get_black_car = "select * from TRUCK_BLACK_NEW where TRUCKNO='" + truckNo + "'";
            DataSet ds_black = ws.getDataSet_Serv(get_black_car);
            return (ds_black.Tables[0].Rows.Count > 0);
        }

        private void textBox_identity_Leave(object sender, EventArgs e)
        {
            string identiry = this.textBox_identity.Text.ToString().Trim();
            if (identiry.Length > 0)
            {
                bool is_black = check_black(identiry);
                if (is_black)
                {
                    MessageBox.Show("此身份证号在黑名单中，不能登记!");
                    return;
                }
            }
        }
        #endregion

        //Check the card status by the car no
        private void check_car_by_no()
        {
            string car_no = this.textBox_carNO.Text.ToString().Trim();
            //string get_car_zf = "select XLH,CARDID,CHEHAO from RFID_READ where CHEHAO='" + car_no + "' and CARDSTATE='0'";

            string get_car = "select XLH,CARDID,CHEHAO,CARDSTATE from RFID_READ where cardstate='0' and  CHEHAO='" + car_no + "' and BS is null";
            DataSet ds_car = ws.getDataSet_Serv(get_car);

            if (ds_car.Tables[0].Rows.Count > 0)
            {
                string card_no = ds_car.Tables[0].Rows[0]["XLH"].ToString().Trim();
                this.textBox_cardNO.Text = card_no;
                this.comboBox_owner.Focus();
                //检查该卡是否已经作废
                string cardstate = ds_car.Tables[0].Rows[0]["CARDSTATE"].ToString();
                if (cardstate == "1")
                {
                    MessageBox.Show("该卡已经作废，请重新发卡!");
                    return;
                }
            }
            else
            {
                //is_new_card = true;
                this.textBox_cardNO.Text = "";
                //this.textBox_carNO.Focus();
                MessageBox.Show("该车没有发卡，请为该车发卡!");
            }
            return;
        }

        private void textBox_carNO_Leave(object sender, EventArgs e)
        {
            string car_no = this.textBox_carNO.Text.ToString().Trim();
            if (car_no.Length > 0)
            {
                bool is_black = check_black(car_no);
                if (is_black)
                {
                    this.groupBox3.Visible = false;
                    MessageBox.Show("此车在黑名单中，不能登记!");
                    return;
                }
            }
            if (car_no.Length > 0)
            {
                check_car_by_no();//check the status of car
            }
            return;
        }
        //cardNO textbox lost focus event handler, when this text box lost its focus, it will
        // get the data from the RFID_READ table by this value, if it can not get any record, then
        //prompt the user to apply one new card, if found at least one record, there are two case:
        //one, the associated car no is empty, then prompt to create new card, two, fill the car no field
        //automatically.
        private void textBox_cardNO_Leave(object sender, EventArgs e)
        {

            string card_no = this.textBox_cardNO.Text.ToString().Trim();
            if (card_no.Length > 0)
            {
                string get_car = "select XLH,CARDID,CHEHAO,CARDSTATE from RFID_READ where cardstate='0' and XLH='" + card_no + "' and BS is null";
                DataSet ds_car = ws.getDataSet_Serv(get_car);

                //DataSet ds_car = get_car_dataset();
                if (ds_car.Tables[0].Rows.Count > 0)
                {

                    string car_no = ds_car.Tables[0].Rows[0]["CHEHAO"].ToString().Trim();
                    if (car_no.Length == 0)
                    {
                        this.textBox_carNO.Text = "";
                        this.textBox_cardNO.Focus();
                        MessageBox.Show("该车没有发卡，请为该车发卡!");
                    }
                    else
                    {
                        //is_new_card = false;
                        this.textBox_carNO.Text = car_no;
                        //this.comboBox_owner.Focus();
                    }
                    //检查该卡是否已经作废
                    string cardstate = ds_car.Tables[0].Rows[0]["CARDSTATE"].ToString();
                    if (cardstate == "1")
                    {
                        MessageBox.Show("该卡已经作废，请重新发卡!");
                        return;
                    }
                }
                else
                {
                    is_new_card = false;
                    this.textBox_cardNO.Text = "";
                    this.textBox_cardNO.Focus();
                    //can not find the record in the RFID_READ table by the serial NO.
                    MessageBox.Show("输入的序列号有误，请重新输入!");
                }

                //带出司机姓名,电话和身份证信息
                //string get_driver_info = "select distinct DRIVERNAME as 姓名,DRIVERID as 身份证,DRIVERPHONE as 电话 from V_TRUCK_CARGO_LOGIN_ALL where QDCARDNUM='" + card_no + "' and  DRIVERNAME is not null";
                string get_driver_info = "select DRIVERNAME as 姓名,DRIVERID as 身份证,DRIVERPHONE as 电话 from (" +
                  "select ROW_NUMBER() OVER(PARTITION BY DRIVERPHONE ORDER BY LOGINTIME desc) as seqNo,t.* from V_TRUCK_CARGO_LOGIN_ALL t where QDCARDNUM = '" + card_no + "' and DRIVERNAME is not null order by logintime desc" +
                  ") where seqNo = 1 and rownum <=3";
                DataSet ds_drivers = ws.getDataSet_Serv(get_driver_info);
                if (ds_drivers.Tables[0].Rows.Count > 0)
                {
                    Drivers divers = new Drivers();
                    Drivers.divers = ds_drivers;
                    divers.ShowDialog(this);
                    this.textBox_name.Text = divers.driver_name;
                    this.textBox_identity.Text = divers.driver_id;
                    this.textBox_phone.Text = divers.driver_phone;
                }
            }
            return;
        }

        //the key press event handler for replacing the tab with Enter key
        #region
        private void textBox_carNO_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar != (char)13)
            {
                return;
            }
            else
            {
                //if (!check_car_no_length())
                //{
                //    MessageBox.Show("车号的位数不正确！");
                //    return;
                //}
                this.comboBox_owner.Focus();
                this.groupBox3.Visible = false;
            }
        }

        private void textBox_cardNO_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)13)
            {
                return;
            }
            else
            {
                this.comboBox_owner.Focus();
                this.groupBox3.Visible = false;
            }
        }

        private void comboBox_owner_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar != (char)13)
            {
                return;
            }
            else
            {
                this.comboBox_stackName.Focus();
            }
        }

        private void comboBox_stackName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)13)
            {
                return;
            }
            else
            {
                this.textBox_billNO.Focus();
            }
        }

        private void textBox_billNO_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)13)
            {
                return;
            }
            else
            {
                this.textBox_material.Focus();
            }
        }

        private void textBox_material_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_specification.Focus();
            }
        }

        private void textBox_specification_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_pieces.Focus();
            }
        }

        private void textBox_pieces_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_tons.Focus();
            }
        }

        private void textBox_tons_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_squares.Focus();
            }
        }

        private void textBox_squares_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                //this.textBox_comment.Focus();
                this.textBox_yard.Focus();
            }
        }
        private void textBox_yard_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                //this.textBox_comment.Focus();
                this.ComboBox_getOwner.Focus();
            }
        }
        private bool check_car_no_length()
        {
            int car_no_lenght = this.textBox_carNO.Text.ToString().Trim().Length;
            if (car_no_lenght == 7)
            {
                return true;
            }
            return false;
        }
        #endregion

        //the specification event handlers
        #region
        private void button_gg_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("ssssss");
            string stack_name = this.comboBox_stackName.Text.ToString();
            int index = stack_name.IndexOf("钢板");
            if (index != -1)
            {
                this.groupBox_gbgg.Left = this.textBox_specification.Left;
                this.groupBox_gbgg.Top = this.button_gg.Top + 200;
                this.groupBox_gbgg.Visible = true;
                this.textBox_gb_hd.Focus();

                //Hide all other specification input window
                this.groupBox_pygg.Visible = false;
                this.groupBox_yg_gg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;
            }
            if (stack_name.IndexOf("盘元") != -1)
            {
                this.groupBox_pygg.Left = this.textBox_specification.Left;
                this.groupBox_pygg.Top = this.button_gg.Top + 200;
                this.groupBox_pygg.Visible = true;
                this.textBox_py_zj.Focus();

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_yg_gg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;

            }
            if (stack_name.IndexOf("圆钢") != -1)
            {
                this.groupBox_yg_gg.Left = this.textBox_specification.Left;
                this.groupBox_yg_gg.Top = this.button_gg.Top + 200;
                this.groupBox_yg_gg.Visible = true;
                this.textBox_yg_zj.Focus();
                this.groupBox_yg_gg.Text = "圆钢规格";

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_pygg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;
            }
            if (stack_name.IndexOf("螺纹钢") != -1)
            {
                this.groupBox_yg_gg.Left = this.textBox_specification.Left;
                this.groupBox_yg_gg.Top = this.button_gg.Top + 200;
                this.groupBox_yg_gg.Visible = true;
                this.textBox_yg_zj.Focus();
                this.groupBox_yg_gg.Text = "螺纹钢规格";

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_pygg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;
            }
            if (stack_name.IndexOf("卷钢") != -1)
            {
                this.groupBox_jg_gg.Left = this.textBox_specification.Left;
                this.groupBox_jg_gg.Top = this.button_gg.Top + 200;
                this.groupBox_jg_gg.Visible = true;
                this.textBox_jg_hd.Focus();

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_pygg.Visible = false;
                this.groupBox_yg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;
            }
            if (stack_name.IndexOf("H钢") != -1)
            {
                this.groupBox_Hg_gg.Left = this.textBox_specification.Left;
                this.groupBox_Hg_gg.Top = this.button_gg.Top + 200;
                this.groupBox_Hg_gg.Visible = true;
                this.textBox_Hg_gd.Focus();

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_pygg.Visible = false;
                this.groupBox_yg_gg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
            }
        }

        private void button_gg_gb_Click(object sender, EventArgs e)
        {
            string gb_hd = this.textBox_gb_hd.Text.ToString().Trim();
            string gb_kd = this.textBox_gb_kd.Text.ToString().Trim();
            string gb_cd = this.textBox_gb_cd.Text.ToString().Trim();
            if (gb_hd.Length == 0 || gb_kd.Length == 0 || gb_cd.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = gb_hd + "mm × " + gb_kd + "mm × " + gb_cd + "mm";
            }
            this.groupBox_gbgg.Visible = false;
            this.textBox_pieces.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string py_zj = this.textBox_py_zj.Text.ToString().Trim();
            if (py_zj.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = "Ф" + py_zj + "mm";
            }
            this.groupBox_pygg.Visible = false;
            this.textBox_pieces.Focus();
        }

        private void button_yg_gg_Click(object sender, EventArgs e)
        {
            string yg_zj = this.textBox_yg_zj.Text.ToString().Trim();
            string yg_cd = this.textBox_yg_cd.Text.ToString().Trim();
            if (yg_zj.Length == 0 || yg_cd.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = "Ф" + yg_zj + "mm × " + yg_cd + "m";
            }
            this.groupBox_yg_gg.Visible = false;
            this.textBox_pieces.Focus();
        }

        private void button_jg_Click(object sender, EventArgs e)
        {
            string jg_hd = this.textBox_jg_hd.Text.ToString().Trim();
            string jg_kd = this.textBox_jg_kd.Text.ToString().Trim();
            if (jg_hd.Length == 0 || jg_hd.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = jg_hd + "mm × " + jg_kd + "mm";
            }
            this.groupBox_jg_gg.Visible = false;
            this.textBox_pieces.Focus();
        }

        private void button_Hg_Click(object sender, EventArgs e)
        {
            string Hg_gd = this.textBox_Hg_gd.Text.ToString().Trim();
            string Hg_kd = this.textBox_Hg_kd.Text.ToString().Trim();
            string Hg_cd = this.textBox_Hg_cd.Text.ToString().Trim();
            if (Hg_gd.Length == 0 || Hg_gd.Length == 0 || Hg_gd.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = Hg_gd + "mm × " + Hg_kd + "mm × " + Hg_cd + "mm";
            }
            this.groupBox_Hg_gg.Visible = false;
            this.textBox_pieces.Focus();
        }

        #endregion

        private void SRCHDJ_KeyDown(object sender, KeyEventArgs e)
        {
            //Show or hide car no input window
            if (e.KeyCode == Keys.D0 && e.Control)
            {
                //Show or hide the car no input little window
                if (this.groupBox3.Visible == false)
                {
                    this.btn_car.PerformClick();//perform the click action of btn_car 
                }
                else
                {
                    this.groupBox3.Visible = false;
                }
            }
            if (e.KeyCode == Keys.NumPad0 && e.Control)
            {
                //Show or hide the car no input little window
                if (this.groupBox3.Visible == false)
                {
                    this.btn_car.PerformClick();//perform the click action of btn_car 
                }
                else
                {
                    this.groupBox3.Visible = false;
                }
            }
            if (e.KeyCode == Keys.F1)
            {
                //Show or hide the car no input little window
                if (this.groupBox3.Visible == false)
                {
                    this.btn_car.PerformClick();//perform the click action of btn_car 
                }
                else
                {
                    this.groupBox3.Visible = false;
                }
            }
            //Show the specification input little window
            if (e.KeyCode == Keys.NumPad1 && e.Control)
            {
                this.button_gg.PerformClick(); //perform the click action of btn_car      
            }
            if (e.KeyCode == Keys.D1 && e.Control)
            {
                this.button_gg.PerformClick(); //perform the click action of btn_car      
            }
            if (e.KeyCode == Keys.F2)
            {
                this.button_gg.PerformClick();
            }
            //Esc for exixt button
            if (e.KeyCode == Keys.Escape)
            {
                this.button_exist.PerformClick();
            }
            //F3 for clear button
            if (e.KeyCode == Keys.F3)
            {
                this.button_clear.PerformClick();
            }
            //F4 for multiple button
            if (e.KeyCode == Keys.F5)
            {
                this.button_multiple.PerformClick();
            }
            //F5 for save button
            if (e.KeyCode == Keys.F6)
            {
                this.button_save.PerformClick();
            }
        }

        private void button_new_card_Click(object sender, EventArgs e)
        {
            FK frm1 = new FK();
            frm1.Left = 600;
            frm1.Top = 300;
            frm1.Text = "新发卡-" + Session.Operator_User.ToString();
            frm1.Show();
        }

        private void load_data()
        {
            string car_no = this.textBox_carNO.Text.ToString().Trim(); 

            string sql = "select logintime as 登记时间,CARDTIME as 确认时间,SHIPPER_NAME as 货主代理,BILL_NO as 提单号,cargoname as 货名,truckno as  车号,qdcardnum as 卡号,DECODE(to_number(TRUCKSTATUS),0,'登记',1,'发指令',2,'确认',3,'已进门',4,'已理货',5,'已出门') as 状态,pieces as 件数,DRIVERPHONE as 电话,cardadd as 登记地点,IOYARDID,TRUCKSTATUS,ID,DYBZ,YARD AS 地点,ZYJX AS 机械 from TRUCK_CARGO_LOGIN" +
                " where (TRUCKSTATUS='0' or TRUCKSTATUS='1' or (TRUCKSTATUS='2' and CARDTIME>=SYSDATE-1.5)) and ioyardid=1 and cardadd='" + card_add + "' order by TRUCKSTATUS,logintime desc";
            DataSet ds = ws.getDataSet_Serv(sql);
            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.DataSource = ds.Tables[0];

            //设置某些字段不可见
            this.dataGridView1.Columns["IOYARDID"].Visible = false;
            this.dataGridView1.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView1.Columns["ID"].Visible = false;
            this.dataGridView1.Columns["DYBZ"].Visible = false;
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow DGVRow in this.dataGridView1.Rows)
            {
                if (DGVRow != null)
                {
                    switch (DGVRow.Cells["TRUCKSTATUS"].Value as string)
                    {
                        case "1":
                            DGVRow.DefaultCellStyle.BackColor = Color.Green;
                            DGVRow.DefaultCellStyle.SelectionForeColor = Color.Green;
                            break;
                        case "2":
                            DGVRow.DefaultCellStyle.BackColor = Color.Fuchsia;
                            DGVRow.DefaultCellStyle.SelectionForeColor = Color.Fuchsia;
                            break;
                        case "3":
                            DGVRow.DefaultCellStyle.BackColor = Color.Orange;
                            DGVRow.DefaultCellStyle.SelectionForeColor = Color.Orange;
                            break;
                        case "4":
                            DGVRow.DefaultCellStyle.BackColor = Color.LightSkyBlue;
                            DGVRow.DefaultCellStyle.SelectionForeColor = Color.LightSkyBlue;
                            break;
                        case "5":
                            DGVRow.DefaultCellStyle.BackColor = Color.Yellow;
                            DGVRow.DefaultCellStyle.SelectionForeColor = Color.Yellow;
                            break;
                    }
                }
            }
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            load_data();
            sx();
        }

        private void button_copy_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {

                string id = this.dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString();
                string sql = "select QDCARDNUM,TRUCKNO,SHIPPER_NAME,SHIPNAME,VOYAGE,BILL_NO,CARGONAME,CARGONO,STACKNAME,YARD,REMARK,DRIVERNAME,DRIVERID,DRIVERPHONE," +
                    "MATERIAL,STANDARDS,PIECES,WEIGHT,CUBE,GETOWNER from TRUCK_CARGO_LOGIN where ID='" + id + "'";
                DataSet ds = ws.getDataSet_Serv(sql);
                DataTable dt = ds.Tables[0];

                string owner = dt.Rows[0]["SHIPPER_NAME"].ToString();
                string bill_no = dt.Rows[0]["BILL_NO"].ToString();

                string yard = dt.Rows[0]["YARD"].ToString();
                string remark = dt.Rows[0]["REMARK"].ToString();

                string material = dt.Rows[0]["MATERIAL"].ToString();
                string standards = dt.Rows[0]["STANDARDS"].ToString();
                string pieces = dt.Rows[0]["PIECES"].ToString();

                string cube = dt.Rows[0]["CUBE"].ToString();

                string weight = dt.Rows[0]["WEIGHT"].ToString();
                string cargoName = dt.Rows[0]["CARGONAME"].ToString();
                string getOwner = dt.Rows[0]["GETOWNER"].ToString();

                SetComboBoxValue(comboBox_owner, owner); 
                
                this.textBox_billNO.Text = bill_no;

                this.textBox_yard.Text = yard;
                this.textBox_comment.Text = remark;

                this.textBox_material.Text = material;
                this.textBox_specification.Text = standards;
                this.textBox_pieces.Text = pieces;

                this.textBox_squares.Text = cube;

                this.textBox_tons.Text = weight;

                SetComboBoxValue(comboBox_stackName, cargoName);
                SetComboBoxValue(ComboBox_getOwner, getOwner); 

                this.textBox_cardNO.Focus(); 
            }
            else
            {
                MessageBox.Show("请选中复制行");
            }

        }

        private void SetComboBoxValue(UCComboBox com, string value)
        {
            com.SelectedText = "";//先置空
            com.SelectedValue = value;
            if (com.SelectedValue == null)
                com.Text = value;
        }

        private void textBox_comment_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_name.Focus();
            }
        }

        private void textBox_name_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_phone.Focus();
            }
        }

        private void textBox_phone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_identity.Focus();
            }
        }
        #region=====================钢板规格回车==========================
        private void textBox_gb_hd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_gb_kd.Focus();
            }
        }

        private void textBox_gb_kd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_gb_cd.Focus();
            }
        }

        private void textBox_gb_cd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_gg_gb.Focus();
            }
        }

        private void button_gg_gb_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_gg_gb.PerformClick();
                //this.textBox_pieces.Focus();
            }
        }
        #endregion
        #region==============盘元规格回车==========================
        private void textBox_py_zj_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button1.Focus();
            }
        }

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button1.PerformClick();
            }
        }
        #endregion
        #region================圆钢规格回车========================
        private void textBox_yg_zj_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_yg_cd.Focus();
            }
        }

        private void textBox_yg_cd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_yg_gg.Focus();
            }
        }
        private void button_yg_gg_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_yg_gg.PerformClick();
            }
        }
        #endregion
        #region=====================卷钢规格回车==================
        private void textBox_jg_hd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_jg_kd.Focus();
            }
        }

        private void textBox_jg_kd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_jg.Focus();
            }
        }

        private void button_jg_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_jg.PerformClick();
            }
        }
        #endregion
        #region===================H钢规格回车=========================
        private void textBox_Hg_gd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_Hg_kd.Focus();
            }
        }

        private void textBox_Hg_kd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_Hg_cd.Focus();
            }
        }

        private void textBox_Hg_cd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_Hg.Focus();
            }
        }

        private void button_Hg_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_Hg.PerformClick();
            }
        }
        #endregion

        private void comboBox_getOwner_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_comment.Focus();
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)//判读是否是鼠标右键
            {
                //this.dataGridView1.ClearSelection();
                //this.dataGridView1.Rows[e.RowIndex].Selected = true;
                this.sendCarToolStripMenuItem.Visible = true;
                this.sendCarToolStripMenuItem.Visible = true;
                contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);//contextMenuStrip1为菜单控件
            }
        }

        private void sendCarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool f = false;
            for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)
            {
                string status = this.dataGridView1.SelectedRows[i].Cells["状态"].Value.ToString();
                if (status == "发指令")
                {
                    string car_no = this.dataGridView1.SelectedRows[i].Cells["车号"].Value.ToString();
                    string login_time = this.dataGridView1.SelectedRows[i].Cells["登记时间"].Value.ToString();
                    string op_user = Session.Operator_User.ToString();
                    String update_sql = "update TRUCK_CARGO_LOGIN set TRUCKSTATUS=2,CARDTIME=sysdate where logintime=to_date('" + login_time + "','yyyy-mm-dd hh24:mi:ss') and TRUCKNO='" + car_no + "' and TRUCKSTATUS<>'9'";
                    string new_id = create_id();
                    string insert_sql = "insert into TRUCK_LOG(ID,TRUCKNO,OPERATENAME,XGSJ,XGNR) values(" + new_id +
                        ",'" + car_no + "','" + op_user + "',sysdate,'停车场放车确认')";
                    int ret1 = ws.execute_UID_Serv(update_sql);
                    int ret2 = ws.execute_UID_Serv(insert_sql);
                    f = true;
                }
            }
            if (f)
            {
                foreach (DataGridViewRow dgvr in this.dataGridView1.SelectedRows)
                {
                    string strid = dgvr.Cells["id"].Value.ToString();
                    string strbj = dgvr.Cells["TRUCKSTATUS"].Value.ToString();
                    string strDYBZ = dgvr.Cells["DYBZ"].Value.ToString();
                    if (strDYBZ.Equals("0") && strbj.Equals("1"))
                    {
                        PrintCode pc = new PrintCode();
                        pc.Run(strid, "大港公司市入车辆登记小票");
                    }
                }
            }
            load_data();
        }

        private void cancelCarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)
            {
                string status = this.dataGridView1.SelectedRows[i].Cells["状态"].Value.ToString();
                if (status == "确认")
                {
                    string car_no = this.dataGridView1.SelectedRows[i].Cells["车号"].Value.ToString();
                    string login_time = this.dataGridView1.SelectedRows[i].Cells["登记时间"].Value.ToString();
                    string op_user = Session.Operator_User.ToString();
                    String update_sql = "update TRUCK_CARGO_LOGIN set TRUCKSTATUS=1 where logintime=to_date('" + login_time + "','yyyy-mm-dd hh24:mi:ss') and TRUCKNO='" + car_no + "' and TRUCKSTATUS<>'9'";
                    string new_id = create_id();
                    string insert_sql = "insert into TRUCK_LOG(ID,TRUCKNO,OPERATENAME,XGSJ,XGNR) values(" + new_id +
                        ",'" + car_no + "','" + op_user + "',sysdate,'停车场取消放车')";
                    int ret1 = ws.execute_UID_Serv(update_sql);
                    int ret2 = ws.execute_UID_Serv(insert_sql);
                }
            }
            load_data();
        }

        private void changeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {

                string id = this.dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString();
                string sql = "select QDCARDNUM,TRUCKNO,SHIPPER_NAME,SHIPNAME,VOYAGE,BILL_NO,CARGONAME,CARGONO,STACKNAME,YARD,REMARK,DRIVERNAME,DRIVERID,DRIVERPHONE," +
                    "MATERIAL,STANDARDS,PIECES,WEIGHT,CUBE,GETOWNER from TRUCK_CARGO_LOGIN where ID='" + id + "'";
                DataSet ds = ws.getDataSet_Serv(sql);
                DataTable dt = ds.Tables[0];
                string card_no = dt.Rows[0]["QDCARDNUM"].ToString();
                string truck_no = dt.Rows[0]["TRUCKNO"].ToString();
                string owner = dt.Rows[0]["SHIPPER_NAME"].ToString();
                string bill_no = dt.Rows[0]["BILL_NO"].ToString();
                string cargo_name = dt.Rows[0]["CARGONAME"].ToString();
                string yard = dt.Rows[0]["YARD"].ToString();
                string remark = dt.Rows[0]["REMARK"].ToString();
                string driver_name = dt.Rows[0]["DRIVERNAME"].ToString();
                string driver_id = dt.Rows[0]["DRIVERID"].ToString();
                string driver_phone = dt.Rows[0]["DRIVERPHONE"].ToString();

                string material = dt.Rows[0]["MATERIAL"].ToString();
                string standards = dt.Rows[0]["STANDARDS"].ToString();
                string pieces = dt.Rows[0]["PIECES"].ToString();
                string weight = dt.Rows[0]["WEIGHT"].ToString();
                string cube = dt.Rows[0]["CUBE"].ToString();
                string get_owner = dt.Rows[0]["GETOWNER"].ToString();

                this.textBox_cardNO.Text = card_no;
                this.textBox_carNO.Text = truck_no;
                this.comboBox_owner.SelectedValue = owner;
                this.textBox_billNO.Text = bill_no;
                this.comboBox_stackName.SelectedValue = cargo_name;
                this.textBox_yard.Text = yard;
                this.textBox_comment.Text = remark;
                this.textBox_name.Text = driver_name;
                this.textBox_identity.Text = driver_id;
                this.textBox_phone.Text = driver_phone;

                this.textBox_material.Text = material;
                this.textBox_specification.Text = standards;
                this.textBox_pieces.Text = pieces;
                this.textBox_tons.Text = weight;
                this.textBox_squares.Text = cube;
                this.ComboBox_getOwner.SelectedValue = get_owner;

                //this.comboBox_owner.SelectedValue = owner;
                is_update = true;
                update_id = id;
            }
        }
        string Str_ID = "";
        string Str_bj = "";
        string Str_DYBZ = "";
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex];
                //dataGridView1.ClearSelection();
                //dataGridViewRow1.Selected = true;
                Str_ID = dataGridViewRow1.Cells["id"].Value.ToString();
                Str_bj = dataGridViewRow1.Cells["TRUCKSTATUS"].Value.ToString();
                Str_DYBZ = dataGridViewRow1.Cells["DYBZ"].Value.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (Str_bj.Equals("2") && Str_DYBZ.Equals("0"))
            {
                PrinForm pf = new PrinForm(Str_ID, "大港公司市入车辆登记小票");
                pf.Show();
            }
            else
            {
                MessageBox.Show(Str_bj + "不能打印小票");
            }
        }

        /// <summary>
        /// 废卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAbandonCard_Click(object sender, EventArgs e)
        {
            using (AbandonCardForm fk = new AbandonCardForm())
            {
                fk.Text = "废卡-" + Session.Operator_User.ToString();
                fk.ShowDialog();
            }
        }

        #region 计算停车费用
        /// <summary>
        /// 显示停车费用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            //CalcFS(login_time, ws.GetSysDateTime().ToString(), car_no);//计算停车费用
            if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridView1.SelectedRows[0];
                string status = row.Cells["状态"].Value.ToString();
                if (status == "确认")
                {
                    string carNo = row.Cells["车号"].Value.ToString();
                    string loginTime = row.Cells["登记时间"].Value.ToString();
                    string commandTime = row.Cells["确认时间"].Value.ToString();

                    string sql = "select TBYBS, TBBBS, YBSF, BBSF, HJ from TRUCK_FS where TRUCKNO = '" + carNo +
                        "' and LOGINTIME = to_date('" + loginTime + "','yyyy-mm-dd hh24:mi:ss') and COMMANDTIME = to_date('" + commandTime + "','yyyy-mm-dd hh24:mi:ss')";
                    DataTable dt = ws.getDataSet_Serv(sql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        DataRow rowFS = dt.Rows[0];
                        ShowTJSF(rowFS["TBBBS"].ToString(), rowFS["TBYBS"].ToString(), rowFS["BBSF"].ToString(), rowFS["YBSF"].ToString(), rowFS["HJ"].ToString());
                    }
                    else
                    {
                        int iDays = 0, iNights = 0;
                        double ybsf = 0, bbsf = 0, hj = 0;
                        CalcFS(loginTime, commandTime, carNo, ref iDays, ref iNights, ref ybsf, ref bbsf, ref hj);
                        ShowTJSF(iDays.ToString(), iNights.ToString(), bbsf.ToString(), ybsf.ToString(), hj.ToString());
                    }
                    pnlTCJF.Visible = true;
                }
                else
                {
                    pnlTCJF.Visible = false;
                }
            }
            else
            {
                pnlTCJF.Visible = false;
            }
        }

        /// <summary>
        /// 显示停车计费
        /// </summary>
        /// <param name="days"></param>
        /// <param name="nights"></param>
        /// <param name="bbsf"></param>
        /// <param name="ybsf"></param>
        /// <param name="hj"></param>
        private void ShowTJSF(string days, string nights, string bbsf, string ybsf, string hj)
        {
            pnlTCJF.Visible = true;
            lblDays.Text = days;
            lblNights.Text = nights;
            lblBBSF.Text = bbsf;
            lblYBSF.Text = ybsf;
            lblHJ.Text = hj;
        }

        const double JFBZ_DAY = 20, JFBZ_NIGHT = 30;//计费标准，白班20， 夜班30

        /// <summary>
        /// 计算停车费用
        /// </summary>
        private void CalcFS(string loginTime, string commandTime, string truckNo, ref int iDays, ref int iNights, ref double ybsf, ref double bbsf, ref double hj)
        {
            DateTime beginTime = DateTime.Parse(loginTime),
                endTime = DateTime.Parse(commandTime),
                eightTime = DateTime.Parse(beginTime.ToString("yyyy-MM-dd 8:00:00")),//早上8点
                eighteenTime = DateTime.Parse(beginTime.ToString("yyyy-MM-dd 18:00:00"));//晚上18点

            double tbsj = endTime.Subtract(beginTime).TotalHours;
            iDays = 0;
            iNights = 0;

            if (beginTime < eightTime)//早8点之前
            {
                iNights++;
                CalcTBSJ(eightTime, endTime, true, ref iDays, ref iNights);
            }
            else if (beginTime < eighteenTime)//早8点到晚16点之间
            {
                iDays++;
                CalcTBSJ(eighteenTime, endTime, false, ref iDays, ref iNights);
            }
            else//晚18点之后
            {
                iNights++;
                eightTime = eightTime.AddDays(1);//第二天早8点
                CalcTBSJ(eightTime, endTime, true, ref iDays, ref iNights);
            }

            ybsf = iNights * JFBZ_NIGHT;
            bbsf = iDays * JFBZ_DAY;
            hj = ybsf + bbsf;

            string id = create_id();
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("INSERT INTO TRUCK_FS(ID, TRUCKNO, LOGINTIME, COMMANDTIME, TBSJ, TBYBS, TBBBS, YBSF, BBSF, HJ) ");
            sbSql.Append("VALUES(" + id + ",'" + truckNo + "',");
            sbSql.Append("to_date('" + loginTime + "','YYYY-MM-DD HH24:MI:SS'),");
            sbSql.Append("to_date('" + commandTime + "','YYYY-MM-DD HH24:MI:SS'),");
            sbSql.Append(tbsj + "," + iNights + "," + iDays + "," + ybsf + "," + bbsf + " ," + hj + ")");
            int ret1 = ws.execute_UID_Serv(sbSql.ToString());
        }

        /// <summary>
        /// 计算停车班数
        /// </summary>
        /// <param name="time"></param>
        /// <param name="endTime"></param>
        /// <param name="isDay"></param>
        /// <param name="days"></param>
        /// <param name="nights"></param>
        private void CalcTBSJ(DateTime time, DateTime endTime, bool isDay, ref int days, ref int nights)
        {
            if (endTime <= time)
            {
                return;
            }
            else
            {
                if (isDay)
                {
                    days++;
                    DateTime tempTime = DateTime.Parse(time.ToString("yyyy-MM-dd 18:00:00"));
                    CalcTBSJ(tempTime, endTime, false, ref days, ref nights);
                }
                else
                {
                    nights++;
                    DateTime tempTime = DateTime.Parse(time.ToString("yyyy-MM-dd 8:00:00")).AddDays(1);
                    CalcTBSJ(tempTime, endTime, true, ref days, ref nights);
                }
            }
        }
        #endregion

        /// <summary>
        /// 修改货代和货信息，多选时信息批量修改成一样的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows == null || this.dataGridView1.SelectedRows.Count <= 0)
                return;
            List<string> listIDs = new List<string>();
            foreach (DataGridViewRow dgvr in this.dataGridView1.SelectedRows)
            {
                if (dgvr.Cells["TRUCKSTATUS"].Value.ToString() == "0")//登记
                {
                    listIDs.Add(dgvr.Cells["ID"].Value.ToString());
                }
            }
            if (listIDs.Count > 0)
            {
                string sql = "select CARGONAME,SHIPPER_NAME,PIECES,WEIGHT,STANDARDS,BILL_NO,MATERIAL,CUBE,YARD,GETOWNER,REMARK from TRUCK_CARGO_LOGIN where ID='" + listIDs[0] + "'";
                DataTable dt = ws.getDataSet_Serv(sql).Tables[0];
                string cargo_name = dt.Rows[0]["CARGONAME"].ToString();
                string owner = dt.Rows[0]["SHIPPER_NAME"].ToString();
                string pieces = dt.Rows[0]["PIECES"].ToString();
                string weight = dt.Rows[0]["WEIGHT"].ToString();
                string standards = dt.Rows[0]["STANDARDS"].ToString();
                string bill_no = dt.Rows[0]["BILL_NO"].ToString();
                string material = dt.Rows[0]["MATERIAL"].ToString();
                string cube = dt.Rows[0]["CUBE"].ToString();
                string yard = dt.Rows[0]["YARD"].ToString();
                string remark = dt.Rows[0]["REMARK"].ToString();
                string get_owner = dt.Rows[0]["GETOWNER"].ToString();

                using (SRModifyForm oForm = new SRModifyForm(owner, cargo_name, bill_no, material, standards, pieces, weight, cube, yard, get_owner, remark))
                {
                    if (oForm.ShowDialog() == DialogResult.OK)
                    {
                        string strSQL = "update truck_cargo_login set CARGONAME='{0}',SHIPPER_NAME='{1}',PIECES='{2}',WEIGHT='{3}',STANDARDS='{4}',BILL_NO='{5}',MATERIAL='{6}',CUBE='{7}',YARD='{8}',GETOWNER='{9}',REMARK='{10}' where id = {11}";
                        foreach (string id in listIDs)
                        {
                            ws.execute_UID_Serv(string.Format(strSQL, oForm.stackname, oForm.shipper, oForm.pieces, oForm.tons, oForm.specification, oForm.billno, oForm.material, oForm.squares, oForm.yard, oForm.getowner, oForm.comment, id));
                        }
                        MessageBox.Show("修改成功！");
                        load_data();
                    }
                }
            }
            else
            {
                MessageBox.Show("只有状态为登记的车辆信息可以修改！");
            }
        }
    }
}
